import { SettingsService } from '@delon/theme';
import { Component, Injector, OnInit, ViewChild } from '@angular/core';
import { PagedListingComponentBase, PagedRequestDto } from "@shared/component-base";
import { HttpClient, HttpParams } from '@angular/common/http';
import { NewsDto, NewsListQueryDto } from "@shared/model/newsdto";
import { NzMessageService, NzModalService } from 'ng-zorro-antd';



@Component({
  templateUrl: './list-news.component.html'
})
export class ListNewsComponent extends PagedListingComponentBase<NewsDto> {
  loading = false;
  dataSet: NewsDto[] = [];
  listQuery: NewsListQueryDto = new NewsListQueryDto();
  constructor(
    private injector: Injector,
    private httpClient: HttpClient,
    private modalService: NzModalService,
    public msg: NzMessageService
  ) {
    super(injector);
  }

  list(request: PagedRequestDto,  finishedCallback: Function): void {
    this.loading = true;
    this.listQuery.pageIndex = request.pageIndex;
    this.listQuery.pageSize = request.pageSize;
    this.listQuery.sortField = this.sortField;
    this.listQuery.sortOrder = this.sortOrder;

    this.httpClient.post("news/pagelist", this.listQuery.toJSON()).subscribe(
      (val) => {
        const body: any = val;
        if (body && !body.success) {
          this.msg.error(body.message);
          return null;
        } else {
          this.dataSet = body.data;
          this.showPaging(body.totalCount, request.pageIndex, body.totalPages);
        }
      },
      response => {
        this.loading = false;
        console.log("Login POST call in error", response);
      },
      () => {
        this.loading = false;
      });
  }

  delete(news: NewsDto): void {
    this.modalService.confirm({
      nzTitle: '你确定删除该条记录吗?',
      nzOnOk: () => {
        let params = new HttpParams().append('id', `${news.id}`);
        this.httpClient.get("news/delete", { params }).subscribe(
          (val) => {
            const result: any = val;
            if (result && !result.success) {
              this.msg.error(result.message);
              return null;
            } else {
              this.msg.success("删除成功！");
              this.refresh();
            }
          });
      }
    });
  }

  create(): void {
    
  }

  edit(news: NewsDto): void {
  }
}

